package com.shop.cereshop.app.tencent.im.model.notice;

import com.alibaba.fastjson2.JSON;
import com.shop.cereshop.commons.tencent.im.enums.notice.SysNoticeTargetTypeEnum;
import com.shop.cereshop.commons.tencent.im.enums.notice.SysNoticeTypeEnum;
import com.shop.cereshop.commons.tencent.im.model.msg.data.CustomMsgDataModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * @description: 发送系统通知请求参数
 * @author zdd
 * @date 2025/9/15 11:21
 * @version 1.0
 */
@ApiModel("发送系统通知请求参数")
@Data
@Accessors(chain = true)
public class SendNoticeParam implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("发送者ID")
    private String senderId;

    @ApiModelProperty("群组ID，用于群发红包")
    private String groupId;

    @ApiModelProperty("接收者ID")
    private String receiverId;

    @ApiModelProperty("接收者名称")
    private String receiverName;

    @ApiModelProperty("类型 (1:评论 2:回复 3:点赞 4:取消点赞 5:收藏 6:取消收藏 7:关注 8:取消关注 9:打赏 10:分享 21:系统公告 22:平台咨询)")
    @NotNull(message = "type不能为空")
    @Min(value = 1, message = "actionType不能小于1")
    @Max(value = 22, message = "actionType不能大于22")
    private Integer type;

    @ApiModelProperty("目标内容类型 (1:视频 2:动态/帖子 3:评论 4:店铺 5:商品)")
    @NotNull(message = "targetType不能为空")
    @Min(value = 1, message = "targetType不能小于1")
    @Max(value = 5, message = "targetType不能大于5")
    private Integer targetType;

    @ApiModelProperty("目标内容唯一ID")
    private String targetId;

    @ApiModelProperty("操作者账号ID")
    private String actorId;

    @ApiModelProperty("操作者昵称")
    private String actorNick;

    @ApiModelProperty("操作者头像URL")
    private String actorAvatar;


    public SystemNoticeMsgDTO covertToSystemNoticeMsgDTO() {
        return SystemNoticeMsgDTO.builder()
                .action(SysNoticeTypeEnum.getNameByCode(this.getType()))
                .targetType(SysNoticeTargetTypeEnum.getNameByCode(this.getTargetType()))
                .targetId(this.getTargetId())
                .actorId(this.getActorId())
                .actorNick(this.getActorNick())
                .actorAvatar(this.getActorAvatar())
                .title(buildNoticeTitle())
                .build();
    }

    /**
     * 构建系统通知自定义消息数据
     * @return 自定义消息数据
     */
    public String covertToMsgData() {
        SystemNoticeMsgDTO noticeMsgDTO = covertToSystemNoticeMsgDTO();
        CustomMsgDataModel<Object> dataModel = new CustomMsgDataModel<>();
        dataModel.setVersion("1.0");
        dataModel.setType(noticeMsgDTO.getAction());
        dataModel.setData(noticeMsgDTO);
        dataModel.setTimestamp(System.currentTimeMillis());
        return JSON.toJSONString(dataModel);
    }

    public String buildNoticeTitle() {
        Integer paramType = this.getType();
        if (paramType > 21) {
            return SysNoticeTypeEnum.getMsgByCode(paramType);
        }
        String actionText = SysNoticeTypeEnum.getMsgByCode(paramType) + "了";
        String targetTypeText = SysNoticeTargetTypeEnum.getMsgByCode(this.getTargetType());
        return this.getActorNick() + " " + actionText + "你的" + targetTypeText;
    }

}
